import { Module } from 'vuex'

import {
  accountLoginRequest,
  requestUserInfoById,
  requestUserMenusByRoleId
} from '@/service/login/login'
// import { mapMenusToRoutes, mapMenusToPermissions } from '@/utlis/map-menus'
import LocalCache from '@/utlis/cache'
import router from '@/router'

import { IAccount } from '@/service/login/type'
import { ILoginState } from './types'
import { IRootState } from '../types'
const LoginModule: Module<ILoginState, IRootState> = {
  namespaced: true,
  state() {
    return {
      token: '',
      userInfo: {},
      userMenus: [],
      permissions: []
    }
  },
  mutations: {
    changeToken(state, token: string) {
      state.token = token
    },
    changeUserInfo(state, userInfo: any) {
      state.userInfo = userInfo
    },
    changeUserMenus(state, userMenus: any) {
      state.userMenus = userMenus

      // userMenus => routes
      // const routes = mapMenusToRoutes(userMenus)
      // 将routes => router.main.children
      // routes.forEach(route => {
      //   router.addRoute('main', route)
      // })

      // 获取用户按钮的权限
      // const permissions = mapMenusToPermissions(userMenus)
      // state.permissions = permissions
    }
  },
  actions: {
    async accountLoginAction({ commit, dispatch }, payload: IAccount) {
      console.log('执行accountLoginAction', payload)

      // 1 实现登录逻辑
      const loginRequest = await accountLoginRequest(payload)
      console.log(loginRequest)
      const { id, token } = loginRequest.data
      commit('changeToken', token)
      // 本地缓存token
      LocalCache.setCache('token', token)

      // 发送初始化的请求(完整的role/department) 在模块里调根里面的方法
      dispatch('getInitalDataAction', null, { root: true })

      // 2.请求用户信息
      const userInfoResult = await requestUserInfoById(id)
      const userInfo = userInfoResult.data
      commit('changeUserInfo', userInfo)
      // 缓存用户信息
      LocalCache.setCache('userInfo', userInfo)

      // 3.请求用户菜单
      const userMenusResult = await requestUserMenusByRoleId(userInfo.role.id)
      if (userMenusResult?.data[0]) {
        userMenusResult.data[0].children =
          userMenusResult.data[0].children.filter((item: any) => item.id != 39)
      }
      const userMenus = userMenusResult.data
      commit('changeUserMenus', userMenus)
      LocalCache.setCache('userMenus', userMenus)

      // 4.跳到首页
      router.push('/')
    },
    // phoneLoginAction({ commit }, payload: any) {
    //   console.log('执行phoneLoginAction')
    // }
    loadLocalLogin({ commit, dispatch }) {
      const token = LocalCache.getCache('token')
      if (token) {
        commit('changeToken', token)

        // 发送初始化的请求(完整的role/department) 在模块里调根里面的方法
        dispatch('getInitalDataAction', null, { root: true })
      }
      const userInfo = LocalCache.getCache('userInfo')
      if (userInfo) {
        commit('changeUserInfo', userInfo)
      }
      const userMenus = LocalCache.getCache('userMenus')
      if (userMenus) {
        commit('changeUserMenus', userMenus)
      }
    }
  },
  getters: {}
}

export default LoginModule
